package com.example.myapplication10;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewDebug;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class chat extends AppCompatActivity {
    public Socket socket=null;
    public boolean connectstate=false;
    public boolean is=false;
    public boolean isget=false;
    ObjectOutputStream oos=null;
    ObjectInputStream ois=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
    }

    public void connect(View view) throws InterruptedException {
        if(!connectstate)
        {
            Thread p=new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        socket=new Socket("110.42.195.30",2000);
                        oos = new ObjectOutputStream(socket.getOutputStream());
                        ois=new ObjectInputStream(socket.getInputStream());
                        connectstate=true;
                    } catch (Exception e) {
                        e.printStackTrace();
                        connectstate=false;
                    }
                }
            });
            p.start();
            p.join();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while(true){
                            TextView show=findViewById(R.id.show);
                            String info=ois.readObject().toString();
                            show.append("你:"+info+'\n');
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
    public void senmessage(View view) {
        is=true;
        if (is) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        EditText message = findViewById(R.id.message);
                        String info = message.getText().toString();
                        oos.writeObject(info);
                        oos.flush();
                        is=false;
                        TextView show=findViewById(R.id.show);
                        show.append("我:"+info+'\n');
                        message.setText(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}